package com.rockcent.mall.lib.helper;

import com.rockcent.mall.lib.domain.MerchantOrder;
import com.rockcent.mall.lib.domain.MerchantOrderItem;
import com.rockcent.mall.lib.domain.MerchantOrderVerification;
import com.rockcent.mall.lib.domain.enumclass.MerchantOrderMerchantRole;
import com.rockcent.mall.lib.domain.enumclass.OrderVerificationWay;
import com.rockcent.mall.lib.domain.enumclass.SettlementStatus;
import com.rockcent.mall.lib.repository.MerchantOrderVerificationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * Created by DK on 16/11/10.
 */
@Component
public class VerificationHelper {

    /**
     * 记录核销记录
     * @param merchantOrder
     * @param merchantOrderItem
     * @param useNum 使用数量
     * @param verifyCode 兑换码
     * @param customerId 客户ID
     * @param logoUrl 产品图片url
     * @param storeName 门店名称
     * @return
     */
    public MerchantOrderVerification merchantOrderVerificationSave(MerchantOrder merchantOrder, MerchantOrderItem merchantOrderItem, Long useNum, String verifyCode, Long customerId, String logoUrl, String storeName, OrderVerificationWay verificationWay, MerchantOrderMerchantRole merchantRole){
        MerchantOrderVerification orderVerification = new MerchantOrderVerification();
        orderVerification.setProductLogo(logoUrl);
        orderVerification.setCustomerId(customerId);
        orderVerification.setMerchantStoreName(storeName);
        orderVerification.setVerificationWay(verificationWay);
        orderVerification.setVerifyCode(verifyCode);
        orderVerification.setVerifyCount(useNum);
        orderVerification.setMerchantRole(merchantRole);

        orderVerification.setCustomerName(merchantOrder.getCustomerName());
        orderVerification.setOrderType(merchantOrder.getOrderType());
        orderVerification.setMallOrderNum(merchantOrder.getMallOrderNum());
        orderVerification.setMerchantStoreId(merchantOrder.getMerchantStoreId());

        orderVerification.setProductId(merchantOrderItem.getProductId());
        orderVerification.setMerchantId(merchantOrderItem.getMerchantId());
        orderVerification.setProductSellPrice(merchantOrderItem.getSingleAmount());
        orderVerification.setMallId(merchantOrderItem.getMallId());
        orderVerification.setMerchantOrderNum(merchantOrderItem.getMerchantOrderNum());
        orderVerification.setMerchantOrderItemId(merchantOrderItem.getId());
        orderVerification.setProductName(merchantOrderItem.getProductName());

        //orderVerification.setVerifyCustomerName(merchantOrder.getCustomerName());
        BigDecimal verifyRate = new BigDecimal(useNum.doubleValue() / merchantOrderItem.getBuyCount().doubleValue() + "");
        BigDecimal verifyPrice = merchantOrderItem.getDiscountAmount().multiply(verifyRate).setScale(2, BigDecimal.ROUND_HALF_UP);
        orderVerification.setVerifyPrice(verifyPrice);
        orderVerification.setSettlementStatus(SettlementStatus.SETTLED);

        return orderVerification;
    }
}
